package com.fang.jdbc;

import com.fang.bean.A;
import com.fang.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Objects;

/**
 * @author fanglingxiao
 * @version 1.0
 * @description TODO
 * @date 2022/4/5 5:05 下午
 **/
public class JdbcTest {
    public static void main(String[] args) throws Exception {
        A a = selectOne();
        System.out.println(a);
    }

    private static A selectOne() throws Exception {
        Connection conn = null;
        try {
            // 注册驱动
            JdbcUtils.registerDriver();
            // 建立链接
            String url = "jdbc:mysql://localhost:3306/fang";
            String userName = "root";
            String password = "12345678";
            conn = JdbcUtils.buildConn(url, userName, password);
            // 获取statement对象
            Statement statement = conn.createStatement();
            // 准备sql
            String sql = "select * from a where id =  1";
            // 执行语句
            ResultSet resultSet = statement.executeQuery(sql);
            // 处理结果集
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                int docId = resultSet.getInt("doc_id");
                int commentId = resultSet.getInt("comment_id");
                A a = new A();
                a.setId(id);
                a.setDocId(docId);
                a.setCommentId(commentId);
                return a;
            }
        } catch (Exception e) {

        } finally {
            // 关闭链接
            if (Objects.nonNull(conn)) {
                conn.close();
            }
        }
        return null;
    }
}
